<?php
class Admin_CaterController extends Ctrl_Admin{
	public function indexAction(){
		$p = $_REQUEST;        
		$tDIMO = new DepartmentinfoModel;
		$tCIMO = new CaterinfoModel;
		$tCMO = new CarteModel;
		$tAMD = new AdminModel;
		$STATUS = 1;
		// $pDepartment = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);

		$tWhere = "1";

		if(!empty($pDepartment)){
            $tWhere .= ' and department = '.$pDepartment;
        }
        if ($_SESSION['admin']['id']!=1) {
    	    $tsql = 'select department from '.$tAMD->table.' where id='.$_SESSION['admin']['id'];
            $tAData = $tAMD->query($tsql);
            $tWhere .= ' and department = '.$tAData[0]['department'];
        }
        $tSql = 'select id,catertime,breakfast,lunch,dinner,other,breakfast_picrute,lunch_picrute,dinner_picrute,other_picture from '.$tCIMO->table.' order by id desc limit 7';
        $tCIDatas = $tCIMO->query($tSql);
       	$tWeek = array("日","一","二","三","四","五","六");
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        	$Department = empty($p['department'])?'':Tool_Fnc::safe_string($p['department']);
        	$catetime = empty($p['catertime'])?'':Tool_Fnc::safe_string($p['catertime']);
			$endtime = empty($p['endtinme'])?'':Tool_Fnc::safe_string($p['endtinme']);
        	$tWher = "1";
        	if(!empty($Department)){
            $tWher .= ' and status = '.$Department;
        	}
        	if (!empty($catetime)&&empty($endtime)) {
				$tWher .= ' and catertime = "'.$catetime.'"';
			}if (!empty($endtime)&&empty($catetime)) {
				$tWher .= ' and catertime = "'.$endtime.'"';
			}if (!empty($endtime)&&!empty($catetime)) {
				$tWher .= ' and catertime between "'.$catetime.'" and "'.$endtime.'"';
			}
        	$tSql = 'select id,catertime,breakfast,lunch,dinner,other,breakfast_picrute,lunch_picrute,dinner_picrute,other_picture,status from '.$tCIMO->table.' where '.$tWher;
        	$tCIDatas = $tCIMO->query($tSql);
        	$STATUS = 2;
        	print_r($Department);	
        }
        foreach($tCIDatas as $key => $Datas){
        	$url =Yaf_Registry::get("config")->web->url->img. '/upload/cater/'.date('Y',strtotime($Datas['catertime'])).'/'.date('m',strtotime($Datas['catertime'])).'/'.date('d',strtotime($Datas['catertime'])).'/';
        	$tCIDatas[$key]['breakfast'] =explode(',',$Datas['breakfast']);
        	$tCIDatas[$key]['lunch'] =explode(',',$Datas['lunch']);
        	$tCIDatas[$key]['dinner'] =explode(',',$Datas['dinner']);
        	$tCIDatas[$key]['other'] =explode(',',$Datas['other']);
        	$tCIDatas[$key]['breakfast_picrute'] =explode(',',$Datas['breakfast_picrute']);
        	$tCIDatas[$key]['lunch_picrute'] =explode(',',$Datas['lunch_picrute']);
        	$tCIDatas[$key]['dinner_picrute'] =explode(',',$Datas['dinner_picrute']);
        	$tCIDatas[$key]['other_picture'] =explode(',',$Datas['other_picture']);
        	$tCIDatas[$key]['url'] = $url;
        	$tCIDatas[$key]['week'] = "星期".$tWeek[date('w',strtotime($Datas['catertime']))];
        }
        $tDIDatas = $tDIMO->field('num,introduce')->fList();
        $this->assign('tCIDatas',$tCIDatas);
        $this->assign('tWeek',$tWeek);
        $this->assign('tDIDatas',$tDIDatas);
        $this->assign('STATUS',$STATUS);
        $this->assign('department',$Department);
        $this->assign('catetime',$catetime);
        $this->assign('endtime',$endtime);

	}
	public function addAction(){
	    $p = $_REQUEST;
	  	$tDIMO = new DepartmentinfoModel;
	   	$tCMO = new CarteModel;
	   	$tCIMO = new CaterinfoModel;
	   	$tAMD = new AdminModel;
	   	$Week = date('w');
	   	$days = 7-$Week;
		for ($i=0; $i <$Week ; $i++) { 
			$W = $Week-$i-1;
			$tDates[$i] = date("Y-m-d",strtotime("-$W day"));
			}
			for ($i=0; $i < $days ; $i++) { 
				 $W = $i+1;
				$tDates[$Week+$i] = date("Y-m-d",strtotime("+$W day"));
		}
        $tDIDatas = $tDIMO->field('num,introduce')->fList();
	    if($_SERVER['REQUEST_METHOD'] == 'POST'){
	    	$tCMO->begin();
		    $pStartTime = empty($p['startcreated'])?Tool_Fnc::ajaxMsg('请过一周再来配餐'):Tool_Fnc::safe_string($p['startcreated']);
		    $pEndTime = empty($p['endcreated'])?Tool_Fnc::ajaxMsg('选择配餐结束时间'):Tool_Fnc::safe_string($p['endcreated']);
		    $pDatetime = empty($p['datetime1'])?'':Tool_Fnc::safe_string($p['datetime1']);
		    $CateIN = $tCIMO->field('*')->where('catertime="'.$pDatetime.'"')->fList();
		    if (!empty($CateIN)) {
		    	Tool_Fnc::ajaxMsg('请您下周再来进行配餐',0);
		    }
		    foreach ($tDIDatas as $key => $IDatas) {
		    	$pDepartment[]= $IDatas['num'];
		    }
		   $pDepartments = implode(',',$pDepartment);
		    $pTime = date('Y-m-d H:i:s',time());
		    $tData = array(
		    	'starttime' => $pStartTime,
				'endtime' => $pEndTime,
				'department' => $pDepartments,
				'createtime' => $pTime,
				'createuser' => $_SESSION['admin']['name'],
		    );
		    if (!$tCMO->insert($tData)) {
		    	Tool_Fnc::ajaxMsg('添加失败001');
		    }
		   	$tSql = "select max(id) from ".$tCMO->table;
		   	$ID = $tCMO->query($tSql);
		   	$pID = $ID[0]["max(id)"];
		   	
		foreach($tDIDatas as $key => $IDatas){
		    for ($i=0; $i < 7; $i++) { 
		   		$pBreakfast  = empty($p['breakfast'.$i])?'':implode(',',$p['breakfast'.$i]);
		    	$pLunch  = empty($p['lunch'.$i])?'':implode(',',$p['lunch'.$i]);
		    	$pDinner  = empty($p['dinner'.$i])?'':implode(',',$p['dinner'.$i]);
		    	$pOther  = empty($p['other'.$i])?'':implode(',',$p['other'.$i]);
		    	$pDatetime = empty($p['datetime'.$i])?'':Tool_Fnc::safe_string($p['datetime'.$i]);
		    	$CateIN = $tCIMO->field('*')->where('catertime="'.$pDatetime.'" and de')->fList();
		    	if (!empty($CateIN)) {
		    		$tCMO->back();
		    		Tool_Fnc::ajaxMsg('请您下周再来进行配餐',0);
		    	}
		     	$tData = array(
		    		'id' => $pID,
		    		'catertime' => $pDatetime,
		    		'breakfast' => $pBreakfast,
		    		'lunch' => $pLunch,
		    		'other' => $pOther,
		    		'dinner' => $pDinner,
		    		'status' => $IDatas['num'],
		    	);
		    	$tCIMO->insert($tData);		    	
		   //  	if(!$tCIMO->insert($tData)){
					// // $tCIMO->back();
					// Tool_Fnc::ajaxMsg('添加失败',0);
		   //  	}
			}
		}
		$tCMO->commit();
		Tool_Fnc::ajaxMsg('添加成功',1);
        }    
        $this->assign('tDates',$tDates);
        $this->assign('tDIDatas',$tDIDatas);
	}
	public function detailAction(){
		$p = $_REQUEST;
		// echo $pID;
		$tCMO = new CarteModel;
		$tCIMO = new CaterinfoModel;
		$tDIMO = new DepartmentinfoModel;
		$tSql = 'select * from '.$tCMO->table.' c left join '.$tCIMO->table.' i on c.id=i.id where c.id='.$p['cid'];
		$tDatas = $tCMO->query($tSql);
		$tWeek = array('周一','周二','周三','周四','周五','周六','周日');
		// print_r($tDatas);die;
		$pTime = date('Ymd',time());
		//print_r($pTime);die;
		foreach ($tDatas as $key => $Datas) {
			$department = $tDIMO->field('introduce')->where('num='.$Datas['department'])->fRow();
			$tDatas[$key]['department'] = $department['introduce'];
			if (str_replace('-', '', $Datas['catertime'])<=$pTime) {
				$tDatas[$key]['status'] = '1';
				
			} $tDatas[$key]['week'] = $tWeek[$key];
		}
		$this->assign('tDatas',$tDatas);
	}
}
